package com.ftg.learn.chapter04;

import org.omg.CORBA.ARG_OUT;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * 学习一下调整为数组
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/5/20 下午1:26
 */
public class LearnArray {

    public static void main(String[] args) {
//        [类型][] [标识符] [=] [new] [类型][长度];
        char[] c1 = new char[3];
        System.out.println(Arrays.toString(c1));
        String a = null;
        System.out.println(a);
        int[] int1 = new int[4];
        int1[0] = 2;
        int1[1] = 2;
        int1[2] = 2;
        int1[3] = 2;
        //默认值
//        {0}{0}{0}{0}

        //{1}{2}{3}{4}{5}{6}
//        int[] int2 = {1, 2, 3, 4, 5, 6};
//        System.out.println(Arrays.toString(int2));
//        for (int i = 1; i < int2.length; i++) {
//            int2[i] = int2[i] + int2[i - 1];
//        }
//        System.out.println(Arrays.toString(int2));
//
//        for (int i = int2.length - 1; i > 0; i--) {
//            int2[i] = int2[i] + int2[i - 1];
//        }
//        System.out.println(Arrays.toString(int2));

//        int[] arr2 = new int[]{2, 5, 1};
//
//        int max = 1;
//        int temp = arr2[max];
//        arr2[max] = arr2[arr2.length - 1];
//        arr2[arr2.length - 1] = temp;
        //加法
//        int c = 9;
//        int b = 13;
//        b = b + c;
//        c = b - c;
//        b = b - c;
//        System.out.println(c);
//        System.out.println(b);
//        //减法
//
//        //积法
//
//        // ^
//        c = c ^ b;
//        b = c ^ b;
//        c = b ^ c;

        //不让使用第三个变量  [空间利用率 | 时间利用率]
//        int temp = c;
//        c = b;
//        b = temp;
//        System.out.println(c);
//        System.out.println(b);

        int[] arr2 = new int[]{2, 5, 1};


        //排序算法

        // 交换
        bubble(arr2);
        System.out.println(Arrays.toString(arr2));
    }

    /**
     * W
     * 冒泡
     * @param arr2
     */
    public static void bubble(int[] arr2) {
        for (int j = 0; j < arr2.length - 1; j++) {
            for (int i = 0; i < arr2.length - 1 - j; i++) {
                if (arr2[i] > arr2[i + 1]) {
                    //swap
                    arr2[i] = arr2[i] + arr2[i + 1];
                    arr2[i + 1] = arr2[i] - arr2[i + 1];
                    arr2[i] = arr2[i] - arr2[i + 1];
                }
            }
        }
    }

    /**
     * 选择排序
     *
     * @param arr2
     */
    public static void choose(int[] arr2) {
        //求几次
        for (int j = 0; j < arr2.length - 1; j++) {

            // get max value
            int maxVal = Integer.MIN_VALUE;
            int index = 0;
            for (int i = 0; i < arr2.length - j; i++) {
                if (maxVal < arr2[i]) {
                    maxVal = arr2[i];
                    index = i;
                }
            }
            //swap
            arr2[index] = arr2[arr2.length - 1 - j];
            arr2[arr2.length - 1 - j] = maxVal;
        }
    }

    //排序
//
//    1 ：基础数据类型
//    2 : 运算符
//    3 : 循环
//    4 : 数组

}
